<?php

class Vattu_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
		$this->_table = $this->config->item('database_tables');
		$this->load->helper('date');
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}

	function get_item_report_this_month()
	{
		$this->db->select('vt_tong_hop.*, vt_item.code, vt_item.name, dvt.name as dvt');
		$this->db->from('vt_tong_hop','vt_item');
		$this->db->join('vt_item','vt_item.id=vt_tong_hop.item_id');
		$this->db->join('dvt','dvt.id=vt_item.id_dvt');
		$this->db->where('MONTH(vt_tong_hop.created_date)',date('m'));
		$this->db->where('YEAR(vt_tong_hop.created_date)',date('Y'));
	
		$query = $this->db->get();
		$rss =  $query->result_array();
	
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->get_total_ps_this_month($rs['item_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	
	// Lấy tồn phát sinh tăng giảm trong tháng hiện tại
	function get_total_ps_this_month($item_id)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('vt_chung_tu_item');
		$this->db->where('vt_chung_tu_item.item_id',$item_id);
		$this->db->where('MONTH(vt_chung_tu_item.created_date)',date('m'));
		$this->db->where('YEAR(vt_chung_tu_item.created_date)',date('Y'));
	
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Public methods
	public function get_all_vat_tu($limit,$start)
	{
		$this->db->select('vt_item.*,dvt.name as dvt');
		$this->db->from('vt_item','dvt');
		$this->db->join('dvt','dvt.id = vt_item.id_dvt');
		$this->db->order_by('vt_item.name', 'asc');
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Public methods
	public function get_all_kho($limit,$start)
	{
		$this->db->select('vt_kho.*, giaovien.magv, giaovien.tengv');
		$this->db->from('vt_kho','giaovien');
		$this->db->join('giaovien','giaovien.user_id = vt_kho.manager_id');
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Public methods
	public function get_all_phieu_nhap($limit,$start)
	{
		$this->db->select('vt_chung_tu.*,vt_kho.name as tenkho');
		$this->db->from('vt_chung_tu','vt_kho');
		$this->db->join('vt_kho','vt_kho.id = vt_chung_tu.id_kho');
		$this->db->where('vt_chung_tu.type',1);
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Public methods
	public function get_all_phieu_xuat($limit,$start)
	{
		$this->db->select('vt_chung_tu.*,vt_kho.name as tenkho');
		$this->db->from('vt_chung_tu','vt_kho');
		$this->db->join('vt_kho','vt_kho.id = vt_chung_tu.id_kho');
		$this->db->where('vt_chung_tu.type',2);
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function count_all_vat_tu()
	{
		$this->db->select('id');
		$this->db->from('vt_item');
		return $this->db->count_all_results();
	}
	
	public function count_all_kho()
	{
		$this->db->select('id');
		$this->db->from('vt_kho');
		return $this->db->count_all_results();
	}
	
	public function count_all_phieu_nhap()
	{
		$this->db->select('id');
		$this->db->from('vt_chung_tu');
		$this->db->where('type',1);
		return $this->db->count_all_results();
	}
	
	public function count_all_phieu_xuat()
	{
		$this->db->select('id');
		$this->db->from('vt_chung_tu');
		$this->db->where('type',2);
		return $this->db->count_all_results();
	}
	
	public function change_status_article($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_content',$data);		
	}
	
}

/* End of file posts_model.php */
/* Location: ./application/modules/admin/models/posts_model.php */